﻿package com.mod.engine  {
	
    import com.calefk.core.Animation;
	import com.calefk.core.EntitySimple;
    import com.calefk.core.GAnimation;
	import com.calefk.modules.math.Vector2D;
	import com.calefk.util.Generic;
	import com.calefk.util.Logger;
	import com.calefk.modules.sysfx.Emitter;
	import flash.display.MovieClip;
	import flash.utils.getTimer;
	import flash.geom.Point;
	import com.calefk.modules.sysfx.SystemFx;
	
	public class Hero extends EntitySimple {
		
		private var emitter:Emitter;
		private var velocity:Vector2D;
		private var mGAnimation:GAnimation;
        
		public function Hero(canvas:MovieClip, nameClip:String = null, 
				px:Number = 0, py:Number = 0) {
			super(canvas, nameClip, px, py);
			
			x = 10;
			y = 200;
            
            var an:Animation;
            mGAnimation = new GAnimation(mClip , null , 0 , 0);
            an = mGAnimation.addAnimation("zombie_die" , 0, 0 , false);
            an.addExecute(47 , endDie);
            
            an = mGAnimation.addAnimation("zombie_run", 0, 0 , true);
            
            // pirmer estado de animacion
            mGAnimation.changeAnimation("zombie_run");
            
			velocity = new Vector2D(4, -5);
			velocity.normalize(6);
			
			SystemFx.instance.createEmitter(null, 
											x,
											y,
                                            270,
											0, 25 , 1 , 0 , 120 , 135 , 1 , 5 , 270 , 0 , "mcParticle" , 1.005 , 1 , 0.015, 1200 , 0 , false, true , false);
											
			SystemFx.instance.createEmitter(null, 
											x,
											y,
                                            270,
											0, 25 , 1 , 0 , 200 , 225 , 1 , 5 , 270 , 0 , "mcParticle" , 1.005 , 1, 0.015, 1200, 0 , false, true , false);								
		
                                            
            onClick = onClickSreen;
        }
        
        private function onClickSreen(rs:*):void {
            mGAnimation.changeAnimation("zombie_die");
        }
        
        private function endDie():void {
            trace("end die");
            mGAnimation.changeAnimation("zombie_run");

        }
		
		override public function update(dt:int):void {
            // actualizo la aniamcion
			if (mGAnimation) {
                mGAnimation.update(dt);
            }
			velocity.setPosition(velocity.x , velocity.y + 0.15);
			
			// move particles
			move(velocity.x , velocity.y);
			
			SystemFx.instance.update(dt);
			
			var emitter0:Emitter = SystemFx.instance.getEmitter(0);
            if (emitter0) {
                emitter0.originX = x;
                emitter0.originY = y;
                emitter0.rotation = velocity.getRad();
			}
			var emitter1:Emitter = SystemFx.instance.getEmitter(1);
            if (emitter1) {
                emitter1.originX = x;
                emitter1.originY = y;
                emitter1.rotation = velocity.getRad();
            }
			
			if (y > 400) {
				velocity.y = -10;
                if (emitter0) {
                    // SystemFx.instance.cleanEmitter(emitter0);
                    
                    // Global.engine.endGame();
                }
                //SystemFx.instance.cleanEmitter(emitter);
				//SystemFx.instance.finalizeEmitter(2);
			}
			
			if (x > 600) {
				velocity.x = -3;
			}
			
			if (x < 0) {
				velocity.x = 6;
			}
		}
		
		override public function clean():void {
			
			super.clean();
		}
		
	}
}